package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.WorkerCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.service.IWorkerCertificationAuditService;
import com.jzo2o.customer.service.IWorkerCertificationService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务人员认证信息表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-06
 */
@Service
public class WorkerCertificationServiceImpl extends ServiceImpl<WorkerCertificationMapper, WorkerCertification> implements IWorkerCertificationService {

    @Autowired
    private IWorkerCertificationAuditService workerCertificationAuditService;



    /**
     * 根据服务人员id更新
     *
     * @param workerCertificationUpdateDTO 服务人员认证更新模型
     */
    @Override
    public void updateById(WorkerCertificationUpdateDTO workerCertificationUpdateDTO) {
        LambdaUpdateWrapper<WorkerCertification> updateWrapper = Wrappers.<WorkerCertification>lambdaUpdate()
                .eq(WorkerCertification::getId, workerCertificationUpdateDTO.getId())
                .set(WorkerCertification::getCertificationStatus, workerCertificationUpdateDTO.getCertificationStatus())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getName()), WorkerCertification::getName, workerCertificationUpdateDTO.getName())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getIdCardNo()), WorkerCertification::getIdCardNo, workerCertificationUpdateDTO.getIdCardNo())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getFrontImg()), WorkerCertification::getFrontImg, workerCertificationUpdateDTO.getFrontImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getBackImg()), WorkerCertification::getBackImg, workerCertificationUpdateDTO.getBackImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationMaterial()), WorkerCertification::getCertificationMaterial, workerCertificationUpdateDTO.getCertificationMaterial())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationTime()), WorkerCertification::getCertificationTime, workerCertificationUpdateDTO.getCertificationTime());
        super.update(updateWrapper);
    }

    /**
     * 查询最新的驳回原因
     */
    @Override
    public String getRejectReason() {
        Long userId = UserContext.currentUserId();
        String result = workerCertificationAuditService.getRejectReason(userId);
        return result;
    }

    /**
     * 提交认证申请
     *
     * @param dto
     */
    @Override
    public void addCertification(WorkerCertificationAuditAddReqDTO dto) {
        //1. 参数校验
        if (dto == null){
            throw new ForbiddenOperationException("参数非法");
        }

        //2. 根据身份证号判断是否认证过
        WorkerCertification workerCertificationDB = this.lambdaQuery().eq(WorkerCertification::getIdCardNo, dto.getIdCardNo()).one();
        if (workerCertificationDB != null && workerCertificationDB.getCertificationStatus() <= 1){
            throw new ForbiddenOperationException("已经提交认证,不能重复提交认证");
        } else if (workerCertificationDB != null && workerCertificationDB.getCertificationStatus() >= 2){
            boolean result = this.removeById(workerCertificationDB.getId());
            if (!result){
                throw new DBException("删除失败");
            }


        }

        //3. 保存认证信息
        WorkerCertification workerCertification = BeanUtil.toBean(dto, WorkerCertification.class);
        workerCertification.setId(UserContext.currentUserId());
        workerCertification.setCertificationTime(LocalDateTime.now());
        boolean result = this.save(workerCertification);
        if (!result){
            throw new DBException("新增认证信息失败");
        }

        //4. 保存认证待审核信息
        WorkerCertification one = this.lambdaQuery().eq(WorkerCertification::getIdCardNo, dto.getIdCardNo()).one();
        // WorkerCertificationAudit audit = new WorkerCertificationAudit();
        WorkerCertificationAudit audit = BeanUtil.toBean(dto, WorkerCertificationAudit.class);
        audit.setServeProviderId(one.getId());
        workerCertificationAuditService.save(audit);
    }
}
